<h1>Edit Time card</h1>

<?php include_partial('form', array('form' => $form)) ?>
<?php foreach ($availableShifts as $cshift):?>
<div id="calculate_<?php echo $cshift->getId() ?>" name="<?php echo $cshift->getId() ?>"><a href="#"><?php echo $cshift->getName() ?></a></div>
<?php endforeach; ?>
<div id="calculate"><a href="#">Calcular con Horario</a></div>
<div id="clear_all"><a href="#">Borrar Todo</a></div>
<form action="<?php echo url_for('timecards/batchUpdateTimeCards') . '?id='.$form->getObject()->getId() ?>"
      method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
<table>
    <thead>
        <th><input type="checkbox" name="master" value="bar" /> </th>
        <th colspan="3">Date</th>
        <th colspan="3">Time Registry</th>
        <th>Position</th>
    </thead>
    <tbody>

    <?php foreach ($timeCardDayForms as $tcdForm): ?>
        <?php if($tcdForm['restDay']->getValue()==1) { $rowClass='freeday';} else {$rowClass='';}?>
        <tr class="<?php echo $rowClass ?>">
            <td><input type="checkbox" name="selRow_<?php echo $tcdForm['id']->getValue() ?>" value="<?php echo $tcdForm['id']->getValue() ?>" /></td>
            <td><?php echo ucfirst(format_date($tcdForm['date']->getValue(), 'EEE')) ?></td>
            <td><?php echo $tcdForm['date']->getValue() ?> </td>
            <td><?php echo $tcdForm['holiday_id']->getValue() ?> </td>
            <td><?php echo $tcdForm['shift1']->render(
                array('id' => 'time_card_day_shift1_' . $tcdForm['id']->getValue(), 'name' =>'shift1[' . $tcdForm['id']->getValue() .']')) ?>
            </td>
            <td><?php echo $tcdForm['shift2']->render(
                        array('id' => 'time_card_day_shift2_' . $tcdForm['id']->getValue(), 'name' =>'shift2[' . $tcdForm['id']->getValue() .']')) ?> </td>
            <td>
                <?php echo $tcdForm['shift3']->render(
                      array('id' => 'time_card_day_shift3_' . $tcdForm['id']->getValue(),
                      'name' =>'shift3[' . $tcdForm['id']->getValue() .']')) ?>
                <span id="ajaxstatus_<?php echo $tcdForm['id']->getValue() ?>"/>
            </td>
            <td>
                <?php echo $tcdForm['position_id'] ?><span class="hidden"><?php echo $tcdForm['position_id']->getValue() ?></span>
               <?php echo $tcdForm['restDay']->render(
                        array('id' => 'restDay_' . $tcdForm['id']->getValue(),
                            'name' =>'restDay[' . $tcdForm['id']->getValue() .']',
                             'type' =>'hidden')) ?>
                 <?php echo $tcdForm['shift_day_id']->render(
                        array('id' => 'shift_day_id_' . $tcdForm['id']->getValue(),
                            'name' =>'shift_day_id[' . $tcdForm['id']->getValue() .']',
                             'type' =>'hidden')) ?>
            </td>
        </tr>
    <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="8"> <input type="submit" value="Save" /></td>
            </tr>
        </tfoot>
        </table>
</form>
        <script type="text/javascript">
        $('input[name=master]').click(function(){
                var chks = $('td input[name^=selRow_]');
                if($(this).is(':checked')){
                    chks.attr('checked', true)
                }else{
                    chks.attr('checked', false)
                }
            }
        );
        $('div[id^=calculate]').click(function(){
            //alert($(this).attr('name'));
            var shift_id = $(this).attr('name');
            var rows = $('tbody tr').filter(function(){
                var chk =  $('td input[name^=selRow_]', this);
                return chk.is(':checked');
            });
                //alert(rows.length);
            rows.each(function(index){
                var cDate = $('td', this).eq(2).text();
                var id =  $('td input[name^=selRow_]', this).attr('value');
                var position_id_td = $('td', this).eq(7);
                var position_id = $('span', position_id_td).text();
//                if(index ==0){
//                    alert('pid ' + position_id);
//                }
                calcUrl = "<?php echo url_for('timecards/getHoursForShift?cdate=') ?>" + cDate
                 + "&position_id=" + position_id
                 + "&shift_id=" + shift_id;
//                  if(index ==0){
//                    alert('url ' + calcUrl);
//                }
                $.ajax({
                    url: calcUrl,
                    type: 'POST',
                    dataType: 'json',
                    timeout: 4000,
                    beforeSend: function(){
                        $('span[id=ajaxstatus_'+ id +']', rows).html("Calculando....");
                    },
                    complete: function(){
                        $('span[id=ajaxstatus_'+ id +']', rows).html("");
                    },
                    error: function(xhr, textStatus, errorThrown){
                        //var msg = "Error " + (error ? errorThrown : xhr.status);
                        var msg = "Error " + errorThrown;
                        $('span[id=ajaxstatus_'+ id +']', rows).html(msg);
                        //alert('Error loading TimeCards');
                    },
                    success: function(data){
                        $('input[id^=time_card_day_shift1_'+ id +']', rows).val(data.hours[0]);
                        $('input[id^=time_card_day_shift2_'+ id +']', rows).val(data.hours[1]);
                        $('input[id^=time_card_day_shift3_'+ id +']', rows).val(data.hours[2]);
                        $('input[id=restDay_'+ id +']', rows).val(data.restday);
                        $('input[id=shift_day_id_'+ id +']', rows).val(data.shiftDayId);
                        var crow = $('input[id^=time_card_day_shift3_'+ id +']', rows).parent().parent();
                        if(data.restday==1){
                            crow.addClass('freeday');
                        }else{
                            crow.removeClass('freeday');
                        }
                    }
                });
            });
           
           
        //        $('input[id^=time_card_day_shift2_]', rows).val('13:00-16:00');
        });

    $('div#clear_all').click(function(){
        $('input[id^=time_card_day_shift1_]').val('');
        $('input[id^=time_card_day_shift2_]').val('');
        //alert("ff");
    });
</script>